package yawning.mew.gui.characters.talents;

import java.util.Iterator;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import yawning.mew.character.Clazz;
import yawning.mew.character.Talent;
import yawning.mew.gui.listener.ToggleButtonListener;
import yawning.mew.gui.tools.CharacterDataSource;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.LayoutStyle.ComponentPlacement;

/**
 * Abstract class that handles the panel for talents
 *
 */
@SuppressWarnings("serial")
public class TalentsPanel extends JPanel {
	protected CharacterDataSource source;
	protected JPanel pnlTalent1;
	protected JPanel pnlTalent2;
	protected JPanel pnlTalent3;
	protected JToggleButton togglePanel1;
	protected JToggleButton togglePanel2;
	private JToggleButton togglePanel3;
	/**
	 * Initialize the base talent panel.
	 */
	public TalentsPanel(CharacterDataSource source) {
		this.source = source;
		if(source.getToon().getChar_Class()==null)
		{
			source.getToon().setChar_Class(Clazz.WARRIOR);//set the toon class to default at warrior, for panel building purposes.
		}
		togglePanel1 = new JToggleButton("");
		togglePanel1.setIcon(new ImageIcon(TalentsPanel.class.getResource("/com/sun/java/swing/plaf/motif/icons/ScrollRightArrow.gif")));
		togglePanel1.setSelectedIcon(new ImageIcon(TalentsPanel.class.getResource("/com/sun/java/swing/plaf/motif/icons/ScrollDownArrowActive.gif")));
		
		togglePanel1.setSelected(true);
		
		pnlTalent1 = new JPanel();
		pnlTalent1.setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),},
			new RowSpec[] {
				RowSpec.decode("20px"),
				RowSpec.decode("20px"),
				RowSpec.decode("20px"),
				RowSpec.decode("20px"),}));
		
		togglePanel2 = new JToggleButton("");
		togglePanel2.setSelected(true);
		togglePanel2.setIcon(new ImageIcon(TalentsPanel.class.getResource("/com/sun/java/swing/plaf/motif/icons/ScrollRightArrow.gif")));
		togglePanel2.setSelectedIcon(new ImageIcon(TalentsPanel.class.getResource("/com/sun/java/swing/plaf/motif/icons/ScrollDownArrowActive.gif")));
		
		pnlTalent2 = new JPanel();
		pnlTalent2.setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),},
			new RowSpec[] {
				RowSpec.decode("20px"),
				RowSpec.decode("20px"),
				RowSpec.decode("20px"),
				RowSpec.decode("20px"),}));
		
		togglePanel3 = new JToggleButton("Hide <dynamic> Tree");
		togglePanel3.setSelected(true);
		togglePanel3.setIcon(new ImageIcon(TalentsPanel.class.getResource("/com/sun/java/swing/plaf/motif/icons/ScrollRightArrow.gif")));
		togglePanel3.setSelectedIcon(new ImageIcon(TalentsPanel.class.getResource("/com/sun/java/swing/plaf/motif/icons/ScrollDownArrowActive.gif")));
		
		pnlTalent3 = new JPanel();
		pnlTalent3.setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),
				ColumnSpec.decode("left:135px"),},
			new RowSpec[] {
				RowSpec.decode("20px"),
				RowSpec.decode("20px"),
				RowSpec.decode("20px"),
				RowSpec.decode("20px"),}));
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(5)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(togglePanel3, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
						.addComponent(togglePanel1)
						.addComponent(pnlTalent1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(togglePanel2)
						.addComponent(pnlTalent2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(pnlTalent3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addGap(22))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(5)
					.addComponent(togglePanel1)
					.addGap(5)
					.addComponent(pnlTalent1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(5)
					.addComponent(togglePanel2)
					.addGap(5)
					.addComponent(pnlTalent2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(togglePanel3, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
					.addGap(5)
					.addComponent(pnlTalent3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(23, Short.MAX_VALUE))
		);
		setLayout(groupLayout);
		
		initTree1();
		initTree2();
		initTree3();
		
	}

	public void notifyClassChange()
	{
		pnlTalent1.removeAll();
		pnlTalent2.removeAll();
		pnlTalent3.removeAll();
		initTree1();
		initTree2();
		initTree3();
	}
	
	private void initTree1()
	{
		Iterator<Talent> talents = source.getAllTalentsFromTree(0);
		String talentTree = source.getTalentTreeName(0);
		togglePanel1.addChangeListener(new ToggleButtonListener(pnlTalent1, "Hide "+ talentTree +" Tree" ,"Show "+talentTree+" Tree"));
		togglePanel1.setText("Hide "+ talentTree +" Tree");
		while(talents.hasNext())
		{
			Talent tal = talents.next();
			TalentContainer talCon = new TalentContainer(tal.toString());
			talCon.setTalent(source);
			
			int col = tal.getColumn()+1;
			int tier = tal.getTier()+1;
			pnlTalent1.add(talCon, tier+", "+ col+", fill, fill");
		}
	}
	
	private void initTree2()
	{
		Iterator<Talent> talents = source.getAllTalentsFromTree(1);
		String talentTree = source.getTalentTreeName(1);
		togglePanel2.addChangeListener(new ToggleButtonListener(pnlTalent2, "Hide "+ talentTree +" Tree" ,"Show "+talentTree+" Tree"));
		togglePanel2.setText("Hide "+ talentTree +" Tree");
		while(talents.hasNext())
		{
			Talent tal = talents.next();
			TalentContainer talCon = new TalentContainer(tal.toString());
			talCon.setTalent(source);
			
			int col = tal.getColumn()+1;
			int tier = tal.getTier()+1;
			pnlTalent2.add(talCon, tier+", "+ col+", fill, fill");
		}
	}
	
	private void initTree3()
	{
		Iterator<Talent> talents = source.getAllTalentsFromTree(2);
		String talentTree = source.getTalentTreeName(2);
		togglePanel3.addChangeListener(new ToggleButtonListener(pnlTalent3, "Hide "+ talentTree +" Tree" ,"Show "+talentTree+" Tree"));
		togglePanel3.setText("Hide "+ talentTree +" Tree");
		while(talents.hasNext())
		{
			Talent tal = talents.next();
			TalentContainer talCon = new TalentContainer(tal.toString());
			talCon.setTalent(source);
			
			int col = tal.getColumn()+1;
			int tier = tal.getTier()+1;
			pnlTalent3.add(talCon, tier+", "+ col+", fill, fill");
		}
	}
	
	public CharacterDataSource getCharacter()
	{
		return source;
	}
}
